#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define Size 7

// 五子棋
//  初始化棋盘
void InitBoard(char (*Board)[Size])
{
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            Board[i][j] = '-';
        }
    }
}

// 打印棋盘
void PrintBoard(char (*Board)[Size])
{
    printf("   ");
    for (int i = 0; i < Size; i++)
    {
        printf("%3d ", i + 1);
    }
    printf("\n");
    for (int i = 0; i < Size; i++)
    {
        printf("%d ", i + 1);
        for (int j = 0; j < Size; j++)
        {
            printf(" %3c", Board[i][j]);
        }
        printf("\n");
    }
}

bool IsVaildPlace(int row, int col)
{
    if (row < 0 || row >= Size || col < 0 || col >= Size)
    {
        return false;
    }
    return true;
}

bool CheckIswin(char (*Board)[Size], int row, int col, char player)
{
    int direction[4][2] =
        {
            {1, 0},
            {1, 1},
            {0, 1},
            {-1, 1}};
    for (int i = 0; i < 4; i++)
    {
        int drow = row + direction[i][0]; // 行
        int dcol = col + direction[i][1]; // 列
        int count = 1;
        while (IsVaildPlace(drow, dcol) && Board[drow][dcol] == player)
        {
            count++;
            if (count == 5)
            {
                return true;
            }
            drow = drow + direction[i][0];
            dcol = dcol + direction[i][1];
        }
        drow = row - direction[i][0]; // 行
        dcol = col - direction[i][1];
        while (IsVaildPlace(drow, dcol) && Board[drow][dcol] == player)
        {
            count++;
            if (count == 5)
            {
                return true;
            }
            drow = drow - direction[i][0];
            dcol = dcol - direction[i][1];
        }
    }
    return false;
}

bool CheckIsDrawn(char (*Board)[Size], char player)
{
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            if (Board[i][j] != '-')
            {
                return false;
            }
        }
    }
    return true;
}

// 判断棋子下在有效位置
bool MakeMove(char (*Board)[Size], int row, int col, char player)
{
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            if (Board[row][col] != '-')
            {
                printf("位置已被占用!请重新输入\n");
                return false;
            }
        }
    }
    if (row >= Size || row < 0 || col >= Size || col < 0)
    {
        printf("位置非法!请重新输入\n");
        return false;
    }
    Board[row][col] = player;
    return true;
}

void AIMakeMove()
{
    // 优先取胜策略
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            if (Board[i][j] == '-')
            {
                Board[i][j] = AI;
                if (CheckIswin(Board, i, j, AI) == true)
                {
                    return true;
                }
                Board[i][j] = '-';
            }
        }
    }

    // 阻止对手策略
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            if (Board[i][j] == '-')
            {
                Board[i][j] = AI;
                if ((CheckIswin(Board, i, j, player) == true) || (CheckIsCount(Board, i, j, player, 4) == true) || (CheckIsCount(Board, i, j, player, 3) == true) || (CheckIsCount(Board, i, j, player, 2) == true))
                {
                    Board[i][j] = AI;
                    return false;
                }
                Board[i][j] = '-';
            }
        }
    }
}

int main()
{
    char Board[Size][Size] = {0};
    InitBoard(Board);
    char player = 'X';
    while (1)
    {
        PrintBoard(Board);
        int row, col;
        printf("请|%c|玩家开始下棋\n", player);
        scanf("%d,%d", &row, &col);
        if (MakeMove(Board, row - 1, col - 1, player) == false)
        {
            continue;
        }
        if (CheckIswin(Board, row - 1, col - 1, player) == true)
        {
            PrintBoard(Board);
            printf("恭喜|%c|赢了\n", player);
            break;
        }
        if (CheckIsDrawn(Board, player) == true)
        {
            PrintBoard(Board);
            printf("平手\n");
            break;
        }
        player = (player == 'X') ? 'O' : 'X';
    }
}
